package companyTest.xiecheng;

import java.util.Arrays;
import java.util.Scanner;

public class Main2 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        Node []nodes=new Node[n];
        for (int i = 0; i < n; i++) {
            Node node=new Node();
            node.s= scanner.next();
            node.value=scanner.nextInt();
            nodes[i]=node;
        }
        Arrays.sort(nodes);
        int max=-1;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j <n ; j++) {
                if (isZichuan(nodes[i].s,nodes[j].s)){
                    int sum=nodes[i].value+nodes[j].value;
                    if (sum>max){
                        max=sum;
                    }
                    break;
                }
            }
        }
        System.out.println(max);
    }
    //判断s1是不是s2子串
    public static boolean isZichuan(String s1,String s2){
        if (s1.length()>s2.length()){
            String temp=s2;
            s2=s1;
            s1=temp;
        }

        for (int i = 0; i < s2.length()-s1.length()+1; i++) {
            if (s1.equals(s2.substring(i,i+s1.length())))return true;
        }
        return false;
    }
}
class Node implements Comparable{
    String s;
    int value;

    @Override
    public int compareTo(Object o) {
        Node o1=(Node)o;
        return o1.value-this.value;
    }
}
